#!/usr/bin/perl

use strict;
use LWP::UserAgent;
use HTTP::Request;
use IO::File;
use Imager;

my $uri = 'https://kaolabo.com/api/detect?apikey=5ac9d12d9c981b84ce777d28621aa58d';

my $request_content;
#my $fh = new IO::File;
#if ($fh->open("< shizucha.jpg")) {
#    do{ $request_content .= $_; }while(<$fh>);
#    $fh->close;
#}
my $img = Imager->new;
#$img->read( file => 'shizucha.jpg' ) or die $img->errstr;
$img->read( file => 'sample3.jpg' ) or die $img->errstr;
$img->write(type=>'jpeg', data=>\$request_content);

my $request = HTTP::Request->new( 'POST' => $uri);

$request->header('Content-Type' => 'image/jpeg');

$request->content($request_content)
  if( $request_content );

my $ua = LWP::UserAgent->new;


my $response = $ua->request($request);
if ( $response->is_success ) {
    $response;
}else{
    warn $response->status_line,"\n";
    $response;
}

my $content =  $response->content;


my $w = $img->getwidth();
my $h = $img->getheight();

$img->rubthrough(
    src => do {
        my $tmp = Imager->new;
        $tmp->read( file => 'musha.jpg' ) or die $tmp->errstr;
        #$tmp->read( file => 'haka.jpg' ) or die $tmp->errstr;
        my $sw = $w/$tmp->getwidth();
        my $sh = $h/$tmp->getheight();
        if( $sw > 1 || $sh > 1 ){
            my $scalefactor = ($sw>$sh)?$sw:$sh;
print $scalefactor." <<<<<<< \n";
            $tmp = $tmp->scale(scalefactor=>$scalefactor);
        }
        $tmp = $tmp->convert(matrix =>[ [ 1, 0, 0, 0 ],
                                 [ 0, 1, 0, 0 ],
                                 [ 0, 0, 1, 0 ],
                                 [ 0, 0, 0, 0.3 ],
                             ]);
        $tmp;
    },
    tx => 0,
    ty => 0,
);


my $face = [];
my $effect = "line";
#my $effect = "box";
while( $content =~ s/<face(.+?)<\/face// ){
    my $node = $1;
    my ($height,$score,$width,$face_x,$face_y,$left_eye_x,$left_eye_y,$right_eye_x,$right_eye_y)
      = (0,0,0,0,0,0,0,0,0);

    ($height,$score,$width,$face_x,$face_y) = ($1,$2,$3,$4,$5)
      if( $node =~ /height="(\d+)" score="(\d+)" width="(\d+)" x="(\d+)" y="(\d+)"/ );

    ($left_eye_x,$left_eye_y) = ($1,$2)
      if( $node =~ /left\-eye x="(\d+)" y="(\d+)"/i );

    ($right_eye_x,$right_eye_y) = ($1,$2)
      if( $node =~ /right\-eye x="(\d+)" y="(\d+)"/i );

    print "\n($height,$score,$width,$face_x,$face_y,$left_eye_x,$left_eye_y,$right_eye_x,$right_eye_y)";
    $img->box(
        xmin => $face_x, ymin => $face_y,
        xmax => $face_x + $width, ymax => $face_y + $height,
        color => '#000000',
        filled => 1,
    ) if( $effect eq "box");

    my $border_h = $height * 0.12;
    $img->box(
#        xmin => $right_eye_x, ymin => $right_eye_y - 10,
#        xmax => $left_eye_x, ymax => $left_eye_y + 10,
        xmin => $face_x, ymin => $right_eye_y - $border_h,
        xmax => $face_x + $width, ymax => $left_eye_y + $border_h,
        color => '#000000',
        filled => 1,
    ) if( $effect eq "line");


}


$img->write( file => 'xyz.jpg', jpegquality => 100 ) or die $img->errstr;


